package com.sky.controller.user;

import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.dto.PageDTO;
import com.sky.query.OrderQuery;
import com.sky.query.PageQuery;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Api(tags = "用户端订单相关接口")
@Slf4j
public class OrderController {
    @Autowired
    private OrdersService ordersService;

    @PostMapping("/submit")
    @ApiOperation("用户下单接口")
    public Result<OrderSubmitVO> submitVO(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("用户下单，参数为：{}",ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = ordersService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public Result<PageDTO<OrderVO>> historyOrders(int page,int pageSize,Integer status){
        log.info("查询历史订单,page:{},pageSize{},status:{}",page,pageSize,status);
        OrderQuery query = new OrderQuery();
        query.setPage(page);
        query.setPageSize(pageSize);
        query.setStatus(status);
        PageDTO<OrderVO> pageDTO = ordersService.queryHistoryOrders(query);
        return Result.success(pageDTO);
    }

    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情接口")
    public Result<OrderVO> orderDetail(@PathVariable Long id){
        log.info("根据id查询订单详情");
        OrderVO orderVO = ordersService.orderDetail(id);
        return Result.success(orderVO);
    }

    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单接口")
    public Result cancelOrder(@PathVariable Long id){
        log.info("根据id取消订单：{}",id);
        ordersService.cancelOrder(id);
        return Result.success();
    }

    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单接口")
    public Result repetition(@PathVariable Long id){
        log.info("再来一单");
        ordersService.repetition(id);
        return Result.success();
    }

    @GetMapping("/reminder/{id}")
    @ApiOperation("顾客催单接口")
    public Result reminder(@PathVariable Long id){
        log.info("顾客催单id：{}",id);
        ordersService.remindOrder(id);
        return Result.success();
    }

}
